package com.example.allshengmedical.experimentnew.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseActivity;
import com.example.allshengmedical.system.activity.NumberFormat3;
import com.example.allshengmedical.util.ByAlert;

import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


//键盘界面
public class KeyboardActivity extends BaseActivity implements OnClickListener {

	private EditText editText;
	// private Button rightthirdBtn;
	// private TextView key_0, key_1, key_2, key_3, key_4, key_5, key_6, key_7,
	// key_8,
	// key_9;
	// private TextView key_a, key_b, key_c, key_d, key_e, key_f, key_g, key_h,
	// key_i,
	// key_j, key_k, key_l, key_m, key_n, key_o, key_p, key_q, key_r,
	// key_s, key_t, key_u, key_v, key_w, key_x, key_y, key_z;
	// private TextView key_shift, key_point, key_del, key_back, key_space,
	// key_enter;
	private TextView title;

	private Boolean shift = false;

	private Button leftfirst_btn, rightthird_btn;

	private final static String ET_DIGITS = "0123456789.";
	private Intent getIntent;

	@Override
	protected int setcontent() {
		return R.layout.activity_keyboard;
	}

	@Override
	protected void initView() {

		getIntent = getIntent();
		editText.setText(getIntent.getStringExtra("folderName"));
		title.setText(getIntent.getStringExtra("title"));
		leftfirst_btn.setVisibility(View.VISIBLE);
		rightthird_btn.setVisibility(View.VISIBLE);
		if (getIntent.getStringExtra("number") != null) {
			if ("number".equals(getIntent.getStringExtra("number"))) {
				editText.setKeyListener(DigitsKeyListener.getInstance(ET_DIGITS));
				final NumberFormat3 numberFormat3 = new NumberFormat3();
				numberFormat3.Format(editText);
			}
		}

		leftfirst_btn.setText("取消");
		rightthird_btn.setText("确认");
		setEditTextInhibitInputSpeChat(editText);
		setEditTextInhibitInputSpace(editText);
		editText.addTextChangedListener(new TextWatcher() {
			String tmp = "";
			String digits = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）_+|{}【】‘；：”“’。，、？]/\\:*?<>|\"\n\t";

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				editText.setSelection(s.length());
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				tmp = s.toString();
			}

			@Override
			public void afterTextChanged(Editable s) {
				String str = s.toString();
				if (str.equals(tmp)) {
					return;
				}
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < str.length(); i++) {
					if (digits.indexOf(str.charAt(i)) < 0) {
						sb.append(str.charAt(i));
					}
				}
				tmp = sb.toString();
				editText.setText(tmp);
			}
		});

		// rightthirdBtn = BindView(R.id.rightthird_btn);
		// rightthirdBtn.setVisibility(View.VISIBLE);
		// rightthirdBtn.setText("close");
		//
		// rightthirdBtn.setOnClickListener(this);
		// key_0.setOnClickListener(this);
		// key_1.setOnClickListener(this);
		// key_2.setOnClickListener(this);
		// key_3.setOnClickListener(this);
		// key_4.setOnClickListener(this);
		// key_5.setOnClickListener(this);
		// key_6.setOnClickListener(this);
		// key_7.setOnClickListener(this);
		// key_8.setOnClickListener(this);
		// key_9.setOnClickListener(this);
		// key_a.setOnClickListener(this);
		// key_b.setOnClickListener(this);
		// key_c.setOnClickListener(this);
		// key_d.setOnClickListener(this);
		// key_e.setOnClickListener(this);
		// key_f.setOnClickListener(this);
		// key_g.setOnClickListener(this);
		// key_h.setOnClickListener(this);
		// key_i.setOnClickListener(this);
		// key_j.setOnClickListener(this);
		// key_k.setOnClickListener(this);
		// key_l.setOnClickListener(this);
		// key_m.setOnClickListener(this);
		// key_n.setOnClickListener(this);
		// key_o.setOnClickListener(this);
		// key_p.setOnClickListener(this);
		// key_q.setOnClickListener(this);
		// key_r.setOnClickListener(this);
		// key_s.setOnClickListener(this);
		// key_t.setOnClickListener(this);
		// key_u.setOnClickListener(this);
		// key_v.setOnClickListener(this);
		// key_w.setOnClickListener(this);
		// key_x.setOnClickListener(this);
		// key_y.setOnClickListener(this);
		// key_z.setOnClickListener(this);
		// key_shift.setOnClickListener(this);
		// key_point.setOnClickListener(this);
		// key_del.setOnClickListener(this);
		// key_back.setOnClickListener(this);
		// key_space.setOnClickListener(this);
		// key_enter.setOnClickListener(this);
		// editText.setFocusable(false);
	}

	@Override
	protected void initData() {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// case R.id.rightthird_btn:
		// finish();
		// break;
		case R.id.key_0:
			editText.setText(editText.getText().append("0"));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_1:
			editText.setText(editText.getText().append("1"));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_2:
			editText.setText(editText.getText().append("2"));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_3:
			editText.setText(editText.getText().append("3"));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_4:
			editText.setText(editText.getText().append("4"));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_5:
			editText.setText(editText.getText().append("5"));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_6:
			Selection.setSelection(editText.getText(), editText.getText().length());
			editText.setText(editText.getText().append("6"));
			break;
		case R.id.key_7:
			editText.setText(editText.getText().append("7"));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_8:
			editText.setText(editText.getText().append("8"));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_9:
			editText.setText(editText.getText().append("9"));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_a:
			if (shift) {
				editText.setText(editText.getText().append("A"));
			} else {
				editText.setText(editText.getText().append("a"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_b:
			if (shift) {
				editText.setText(editText.getText().append("B"));
			} else {
				editText.setText(editText.getText().append("b"));
			}
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_c:
			if (shift) {
				editText.setText(editText.getText().append("C"));
			} else {
				editText.setText(editText.getText().append("c"));
			}
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_d:
			if (shift) {
				editText.setText(editText.getText().append("D"));
			} else {
				editText.setText(editText.getText().append("d"));
			}

			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_e:
			if (shift) {
				editText.setText(editText.getText().append("E"));
			} else {

				editText.setText(editText.getText().append("e"));
			}
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_f:
			if (shift) {
				editText.setText(editText.getText().append("F"));
			} else {

				editText.setText(editText.getText().append("f"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_g:
			if (shift) {
				editText.setText(editText.getText().append("G"));
			} else {

				editText.setText(editText.getText().append("g"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_h:
			if (shift) {
				editText.setText(editText.getText().append("H"));
			} else {

				editText.setText(editText.getText().append("h"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_i:
			if (shift) {
				editText.setText(editText.getText().append("I"));
			} else {
				editText.setText(editText.getText().append("i"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_j:
			if (shift) {
				editText.setText(editText.getText().append("J"));
			} else {

				editText.setText(editText.getText().append("j"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_k:
			if (shift) {
				editText.setText(editText.getText().append("K"));
			} else {

				editText.setText(editText.getText().append("k"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_l:
			if (shift) {
				editText.setText(editText.getText().append("L"));
			} else {

				editText.setText(editText.getText().append("l"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_m:
			if (shift) {
				editText.setText(editText.getText().append("M"));
			} else {

				editText.setText(editText.getText().append("m"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_n:
			if (shift) {
				editText.setText(editText.getText().append("N"));
			} else {

				editText.setText(editText.getText().append("n"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_o:
			if (shift) {
				editText.setText(editText.getText().append("O"));
			} else {

				editText.setText(editText.getText().append("o"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_p:
			if (shift) {
				editText.setText(editText.getText().append("P"));
			} else {

				editText.setText(editText.getText().append("p"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_q:
			if (shift) {
				editText.setText(editText.getText().append("Q"));
			} else {

				editText.setText(editText.getText().append("q"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_r:
			if (shift) {
				editText.setText(editText.getText().append("R"));
			} else {

				editText.setText(editText.getText().append("r"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_s:
			if (shift) {
				editText.setText(editText.getText().append("S"));
			} else {

				editText.setText(editText.getText().append("s"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_t:
			if (shift) {
				editText.setText(editText.getText().append("T"));
			} else {

				editText.setText(editText.getText().append("t"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_u:
			if (shift) {
				editText.setText(editText.getText().append("U"));
			} else {

				editText.setText(editText.getText().append("u"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_v:
			if (shift) {
				editText.setText(editText.getText().append("V"));
			} else {

				editText.setText(editText.getText().append("v"));
			}
			shift = false;

			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_w:
			if (shift) {
				editText.setText(editText.getText().append("W"));
			} else {

				editText.setText(editText.getText().append("w"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_x:
			if (shift) {
				editText.setText(editText.getText().append("X"));
			} else {

				editText.setText(editText.getText().append("x"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_y:
			if (shift) {
				editText.setText(editText.getText().append("Y"));
			} else {

				editText.setText(editText.getText().append("y"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_z:
			if (shift) {
				editText.setText(editText.getText().append("Z"));
			} else {

				editText.setText(editText.getText().append("z"));
			}
			shift = false;
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_shift:

			shift = !shift;
			break;
		case R.id.key_point:
			editText.setText(editText.getText().append("."));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_del:
			if (editText.getText().length() != 0) {
				editText.setText(editText.getText().subSequence(0, editText.getText().length() - 1));
				Selection.setSelection(editText.getText(), editText.getText().length());
			}
			break;
		case R.id.key_back:
			// Intent backIntent = new Intent();
			// // 把返回数据存入Intent
			// backIntent.putExtra("result", "");
			// // 设置返回数据
			// setResult(202, backIntent);
			// // 关闭Activity
			// finish();
			break;
		case R.id.key_space:
			editText.setText(editText.getText().append(" "));
			Selection.setSelection(editText.getText(), editText.getText().length());
			break;
		case R.id.key_enter:
			// Intent enterIntent = new Intent();
			// // 把返回数据存入Intent
			// enterIntent.putExtra("result", editText.getText() + "");
			// // 设置返回数据
			// this.setResult(201, enterIntent);
			// // 关闭Activity
			// finish();
			// break;

			// leftfirst_btn,rightforth_btn

		case R.id.rightthird_btn:

			if (getText(R.string.file_name).equals(title.getText().toString())) {
				// 新建文件名
				if (editText.getText().toString().length() > 24) {
					ByAlert.alert("文件名称不能超过24位！");
					return;
				}
			} else if (getText(R.string.folder_name).equals(title.getText().toString())) {
				// 新建文件夹名称
				if (editText.getText().toString().length() > 10) {
					ByAlert.alert("文件夹名称不能超过10位！");
					return;
				}
			} else if (getText(R.string.experiment_name).equals(title.getText().toString())) {
				// baseInformation
				if (editText.getText().toString().length() > 24) {
					ByAlert.alert("实验名称不能超过24位！");
					return;
				}
			} else if (getText(R.string.experiment_id).equals(title.getText().toString())) {
				// baseInformation
				if (editText.getText().toString().length() > 24) {
					ByAlert.alert("实验ID不能超过24位！");
					return;
				}
			} else if (getText(R.string.experimenter).equals(title.getText().toString())) {
				// baseInformation
				if (editText.getText().toString().length() > 24) {
					ByAlert.alert("实验人员不能超过24位！");
					return;
				}
			} else if (getText(R.string.target_name).equals(title.getText().toString())) {
				//
				if (editText.getText().toString().length() > 9) {
					ByAlert.alert("目标基因名称不能超过9位！");
					return;
				}
			} else if (getText(R.string.sample_name).equals(title.getText().toString())) {
				//
				if (editText.getText().toString().length() > 9) {
					ByAlert.alert("样品不能超过9位！");
					return;
				}
			} else if (getText(R.string.add_a_new_folder).equals(title.getText().toString())) {
				//
				// 新建文件夹名称
				if (editText.getText().toString().length() > 10) {
					ByAlert.alert("文件夹名称不能超过10位！");
					return;
				}
			}

			Intent enterIntent = new Intent();
			// 把返回数据存入Intent
			enterIntent.putExtra("result", editText.getText() + "");
			enterIntent.putExtra("oldName", getIntent.getStringExtra("folderName") + "");
			
			// 设置返回数据
			this.setResult(201, enterIntent);
			// 关闭Activity
			finish();
			break;
		case R.id.leftfirst_btn:
			Intent backIntent = new Intent();
			// 把返回数据存入Intent
			backIntent.putExtra("result", "");
			// 设置返回数据
			setResult(202, backIntent);
			// 关闭Activity
			finish();
			break;
		default:
			break;
		}
	}

	/**
	 * 禁止EditText输入特殊字符
	 * 
	 * @param editText
	 */
	public static void setEditTextInhibitInputSpeChat(EditText editText) {

		InputFilter filter = new InputFilter() {
			@Override
			public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
				String speChat = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）_+|{}【】‘；：”“’。，、？]";
				Pattern pattern = Pattern.compile(speChat);
				Matcher matcher = pattern.matcher(source.toString());
				if (matcher.find())
					return "";
				else
					return null;
			}
		};
		editText.setFilters(new InputFilter[] { filter });
	}

	/**
	 * 禁止EditText输入空格
	 * 
	 * @param editText
	 */
	public static void setEditTextInhibitInputSpace(EditText editText) {
		InputFilter filter = new InputFilter() {
			@Override
			public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
				if (source.equals(" ")) {
					return "";
				} else
					return null;
			}
		};
		editText.setFilters(new InputFilter[] { filter });
	}
}
